package com.applink.sell_proof.database.table;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.Country;

public class CountryDBHelper {
	public static final String TABLE_NAME = "country_table";
	public static final String COUNTRY_ID = "contry_code";
	public static final String COUNTRY_NAME = "country_name";
	public static final String POSTAL_CODE = "postal_code";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ COUNTRY_ID + " integer primary key ," + POSTAL_CODE + " text ,"
			+ COUNTRY_NAME + " text " + ");";

	public static void createCountry(final Country country) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(COUNTRY_ID, country.getCode());
				value.put(COUNTRY_NAME, country.getName());
				value.put(POSTAL_CODE, country.getPostalCode());
				resolver.insert(EMarketingContentProvider.COUNTRY_CONTENT_URI,
						value);
			}

		}.start();
	}

	public static List<Country> getAllCountry() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.COUNTRY_CONTENT_URI, null, null,
				null, null);

		if (cursor != null) {
			List<Country> countries = new ArrayList<Country>();
			while (cursor.moveToNext()) {
				Country country = new Country();
				country.setCode(cursor.getInt(cursor.getColumnIndex(COUNTRY_ID)));
				country.setName(cursor.getString(cursor
						.getColumnIndex(COUNTRY_NAME)));
				country.setPostalCode(cursor.getString(cursor
						.getColumnIndex(POSTAL_CODE)));
				countries.add(country);
			}
			return countries.size() > 0 ? countries : null;
		}
		return null;
	}

	public static Country getCountryById(int id) {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();

		Cursor cursor = resolver.query(
				EMarketingContentProvider.COUNTRY_CONTENT_URI, null, COUNTRY_ID
						+ "=?", new String[] { Integer.toString(id) }, null);

		if (cursor != null && cursor.moveToFirst()) {
			Country country = new Country();
			country.setCode(id);
			country.setName(cursor.getString(cursor
					.getColumnIndex(COUNTRY_NAME)));
			country.setPostalCode(cursor.getString(cursor
					.getColumnIndex(POSTAL_CODE)));
			cursor.close();
			return country;
		}
		return null;
	}

	public static int getCountryPosition(String countryName,
			List<Country> sourceparam) {
		int returnPos = 0;

		for (int index = 0; index < sourceparam.size(); index++) {
			Country temp = sourceparam.get(index);
			if (temp.getName() == countryName) {
				returnPos = index;
				break;
			}
		}
		return returnPos;
	}
}
